<?php

namespace Api\Model;


class ExamRecordModel extends BaseModel{

	public static $RESULT_RIGHT = 1;
    public static $RESULT_WRONG = 2;

	public function getIdsByUidAndMid($uid, $mid){
		if (!$uid || !$mid) {
            return ['status' => 0, 'data' => '参数为空'];
        }
        $question_items = $this->where(['uid' => $uid, 'mid' => $mid, 'result' => self::$RESULT_WRONG, 'state' => 1])->field('qid as id')->select();
        return $question_items;
	}

    public function getIdsByMidAndUid($uid, $mid){
        if (!$uid || !$mid) {
            return ['status' => 0, 'data' => '参数为空'];
        }
        $question_items = $this->where(['uid' => $uid, 'mid' => $mid, 'state' => 1])->field('qid as id')->select();
        return $question_items;
    }


	public function getByUidAndQids ($uid, $qids, $mid)
    {
        if (!$uid || !$mid || count($qids) == 0) {
            return ['success' => FALSE, '参数错误'];
        }

        // todo 考虑添加缓存机制 ，但是这张表又读又写 不好分离
        $question_record = $this->where(['uid' => $uid,'mid' => $mid, 'qid' => ['IN', implode(',', $qids)]])->select();
        return $question_record;
    }
	

    public function deleteByUidAndMid($uid, $mid)
    {
        if (!$uid || !$mid) {
            return ['success' => FALSE, '参数错误'];
        }

        $qid_items = $this->where(['uid' => $uid, 'mid' => $mid])->setField('state',0);
        return $qid_items;
    }
}